<template>
 <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 警告区域 -->
      <el-alert
      title="注意：只允许为第三级分类设置相关参数！"
      type="warning"
      show-icon
      :closable="false">
      </el-alert>
      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
          <el-col>
            <span>选择商品分类：</span>
            <el-cascader
            class="caspanel"
             :options="catelist"
             :props="cateProps"
             expand-trigger='hover'
             v-model='selectedCateKeys'
             @change="handleChange"

             ></el-cascader>
          </el-col>
      </el-row>
      <!--  tabs页签-->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <!-- 添加动态参数的面板 -->
          <el-tab-pane label="动态参数" name="many">
            <el-button type="primary" size="mini" :disabled='isBtnDisabled'>添加参数</el-button>
            <!-- 动态参数列表 -->
            <el-table :data='manyTableData' border stripe>
              <!-- 展开 -->
              <el-table-column type='expand'></el-table-column>
              <!-- 索引列 -->
              <el-table-column type='index'></el-table-column>
              <el-table-column label="参数名称" prop="attr_name"></el-table-column>
              <el-table-column label="操作" >
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 添加静态属性的面板 -->
          <el-tab-pane label="静态属性" name="only">
            <el-button type="primary" size="mini" :disabled='isBtnDisabled'>添加属性</el-button>
          <!-- 静态面板 -->
           <el-table :data='onlyTableData' border stripe>
              <!-- 展开 -->
              <el-table-column type='expand'></el-table-column>
              <!-- 索引列 -->
              <el-table-column type='index'></el-table-column>
              <el-table-column label="属性名称" prop="attr_name"></el-table-column>
              <el-table-column label="操作" >
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          </el-tabs>
    </el-card>

  </div>
</template>

<script>

export default {
  data () {
    return {
      // 商品分类列表
      catelist: [],
      // 级联选择框的配置
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 级联
      selectedCateKeys: [],
      activeName: 'many',
      manyTableData: [],
      onlyTableData: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取所有的商品分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) return this.$message.error('获取商品分类失败')
      this.catelist = res.data
      // console.log(this.catelist)
    },
    // 级联选择框变化函数触发
    handleChange () {
      this.getParamsData()
    },
    // tab点击事件处理
    handleTabClick () {
      // console.log(this.activeName)
      this.getParamsData()
    },
    async getParamsData () {
      if (this.selectedCateKeys.length !== 3) {
        this.selectedCateKeys = []
        return
      }
      // 打印三级分类
      // console.log(this.selectedCateKeys)
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`,
        { params: { sel: this.activeName } })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      if (this.activeName === 'many') {
        // 动态参数
        this.manyTableData = res.data
      }
      // 静态参数
      this.onlyTableData = res.data
    }
  },
  computed: {
    // 按钮需要被禁用返回true
    isBtnDisabled () {
      if (this.selectedCateKeys.length !== 3) return true
      return false
    },
    cateId () {
      if (this.selectedCateKeys.length === 3) { return this.selectedCateKeys[2] }
      return null
    }

  }

}
</script>

<style lang="less" scoped>
  .cat_opt{
    margin: 15px 0;
  }

</style>
